#ifndef SEGMENTSMERGER_H
#define SEGMENTSMERGER_H

#include <QObject>
#include <QProcess>
#include <QStringList>

class SegmentsMerger : public QObject
{
    Q_OBJECT
public:
    enum Error {
        CREATE_MERGE_FILELIST_ERROR = 10000,
        OPEN_MERGE_FILELIST_ERROR,
        NO_SEGMENTS_ERROR,
        INVALID_SEGMENTS_ERROR,
        INVALID_OUTPUT_FILE_ERROR,
        MERGE_TIMEOUT_ERROR,
        START_MERGE_PROCESS_ERROR,
        MERGE_PROCESS_ERROR,
        MERGE_PROCESS_EXIT_ERROR,
        NO_VALID_SEGMENTS_ERROR
    };

    explicit SegmentsMerger(QObject* parent = nullptr);
    ~SegmentsMerger();

    bool mergeSegments(const QString& segmentsDir, const QString& outputPath,
                       const QStringList& segmentFiles);

signals:
    void progressChanged(int progress);
    void finished(const QString& outputPath);
    void errorOccurred(int error, const QString& message);

private:
    bool createFileList(const QString& segmentsDir, const QString& listFilePath,
                        const QStringList& segmentFiles);
    bool validateSegments(const QString& segmentsDir, const QStringList& segmentFiles);
    void cleanup(const QString& segmentsDir);
    void handleProcessError(QProcess::ProcessError error);
    void handleProcessExit(int exitCode, QProcess::ExitStatus exitStatus);

    QProcess* m_process = nullptr;
    QString m_outputPath;
};

#endif // SEGMENTSMERGER_H
